package edu.guigu.appquickstart.recyclerview.ex2;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;

import java.util.ArrayList;

import edu.guigu.appquickstart.R;
import edu.guigu.appquickstart.recyclerview.bean.PersonBean;

/**
 * @author yuanc
 * @Description: (用一句话描述)
 * @date 2025/8/6 11:17
 */
public class PersonBeanAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
    private Context context;
    private ArrayList<PersonBean> dataList;

    public PersonBeanAdapter(Context context, ArrayList<PersonBean> dataList) {
        this.context = context;
        this.dataList = dataList;
    }

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View itemView = LayoutInflater.from(context).inflate(R.layout.item_data_list, parent, false);
        return new MyViewHolder(itemView);
    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
        PersonBean personBean = dataList.get(position);
        MyViewHolder viewHolder = (MyViewHolder) holder;


        viewHolder.iv_check.setVisibility(View.GONE);
        if(PersonBean.isShow){
            viewHolder.iv_check.setVisibility(View.VISIBLE);

            if(personBean.isChecked()){
                viewHolder.iv_check.setImageResource(R.drawable.icon_choose_selected);
            }else{
                viewHolder.iv_check.setImageResource(R.drawable.icon_choose_default);
            }
        }


        viewHolder.tv_video_info.setText(personBean.getDesc());
        Glide.with(context)
                .load(personBean.getUrl())
                .placeholder(R.drawable.ic_loading)
                .error(R.drawable.ic_image)
                .centerCrop()
                .into(viewHolder.iv_img);

    }

    @Override
    public int getItemCount() {
        return dataList.size();
    }

    class MyViewHolder extends RecyclerView.ViewHolder {
        ImageView iv_check;
        ImageView iv_img;
        TextView tv_video_info;

        public MyViewHolder(@NonNull View itemView) {
            super(itemView);
            iv_check = itemView.findViewById(R.id.iv_check);
            iv_img = itemView.findViewById(R.id.iv_img);
            tv_video_info = itemView.findViewById(R.id.tv_video_info);

            itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if(onClickListener!=null){
                        onClickListener.onClick(v, getAdapterPosition());
                    }
                }
            });
        }
    }

    private OnMyItemViewOnClickListener onClickListener;

    public void setOnMyItemViewOnClickListener(OnMyItemViewOnClickListener onClickListener) {
        this.onClickListener = onClickListener;
    }

    interface OnMyItemViewOnClickListener {
        void onClick(View view, int position);
    }
}
